package org.taobao.lxw.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.taobao.dq.bean.User;
import org.taobao.lxw.bean.Comments;
import org.taobao.lxw.bean.Goods;
import org.taobao.lxw.bean.HotSpot;
import org.taobao.lxw.service.ICommentService;
import org.taobao.lxw.service.IGoodsService;

import com.google.gson.Gson;

@Controller
@RequestMapping("/business4")
public class CommentController {
	@Autowired
	ICommentService service;
	@Autowired
	IGoodsService service2;
	Integer storeId;
	private Integer pageIndex;

	@RequestMapping("/getcommentscount")
	@ResponseBody
	public String getGoodsCount(HttpServletRequest request) {
		User business = (User) request.getSession().getAttribute("user");
		storeId = service2.getStoreIdById(business.getId());
		return service.getCommentsCountBySid(storeId) + "";

	}

	// 根据店铺id来查找所有评价
	@RequestMapping(value = "/getallcomments", produces = "text/plain;charset=utf-8")
	@ResponseBody
	public String getAllComments(Integer pageIndex, Integer pageSize) {
		this.pageIndex = pageIndex;
		List<Comments> list = service.getAllCommentPageById(storeId, pageIndex, pageSize);
		Gson gson = new Gson();
		String msg = gson.toJson(list);
		return msg;
	}

	/**
	 * 通过商品id来查询商品并返回给修改页面
	 * 
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping("/getcommentbyid")
	public String getCommentById(Integer id, Model model) {
		Comments info = service.getCommentById(id);
		model.addAttribute("info", info);
		return "forward:/background/business/commentMsg.jsp";
	}

	/**
	 * 通过评价id来查找评价返回评价列表
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/selectcommentbyid", produces = "text/plain;charset=utf-8")
	@ResponseBody
	public String selectCommentByIdAndSid(Integer id) {
		Comments info = service.getCommentByIdAndSid(id,storeId);
		Gson gson = new Gson();
		String msg = gson.toJson(info);
		return msg;
	}

	@RequestMapping(value = "/selectcommentsbyname", produces = "text/plain;charset=utf-8")
	@ResponseBody
	public String getCommentsByName(String name) {

		List<Comments> list = service.getCommentsByName(storeId,name);
		Gson gson = new Gson();
		String msg = gson.toJson(list);

		return msg;
	}
	
	@RequestMapping(value = "/selectcommentsbyoid", produces = "text/plain;charset=utf-8")
	@ResponseBody
	public String getCommentsByOid(Integer oid) {

		List<Comments> list = service.getCommentByOid(oid);
		Gson gson = new Gson();
		String msg = gson.toJson(list);
		return msg;
	}

}
